DIM
[Command] - Universal declaration of Variables and arrays
Syntax:
DIM variablename[,variablename] AS variabletype[=value] [AT Address]
Parameters:
variablename: name of a variable
variabletype: any variable type like BYTE, UBYTE, STRING, etc. If type is not specified, FLOAT will be used, unless you use a suffix (sigil) like $ or %.
Adress: Memory Adress or pointer
Description:
DIM is used in Sinclair BASIC to declare arrays. In ZX BASIC, its usage has been extended to declare any variable and its type. A type is a name for the kind of data (Integer, Byte, String, etc.) it holds.
ZX BASIC will initialize any numeric variable to 0 (like most BASIC flavors), and any string variable to an empty string, so you don't need to initialize them, though it's recommended.
ZX BASIC allows you to use undeclared variables. In Sinclair BASIC, using an unassigned variable triggered the error Variable not found.
When you use an undeclared variable, ZX BASIC will try to guess its type by looking at the context in which it is being used and then will initialize it with a default value, depending on the type (0 or an empty string). If it cannot guess the type (this is usually very difficult), it will fallback to float. The float type is the most inefficient (though most flexible) type ZX BASIC supports, but it is the Sinclair BASIC compatible one. So if you want the compiler to make an efficient and optimized compilation, it is better to declare the variable types you use in advance using the DIM statement. (Note that languages like C or Pascal requires every used variable to be declared).
Declaring a variable that has already been referenced in previous lines will result in a syntax error.
Examples:
REM ' Declares 'a' as a 16 bit signed integer variable DIM a AS INTEGER REM ' Declares 'b' as a Float because no type is specified DIM b REM ' Declares 'c' as String, because of the '$' suffix DIM c$ REM ' Declares d as String, using an explicit type DIM d AS STRING REM ' Declares x, y as 32bit unsigned integers in a single line DIM x, y AS ULONG REM ' Here S is declared as String, because R has a '$' DIM R$, S REM ' initialize an unsigned byte with 5 DIM b AS UBYTE = 5 REM ' warning: Using default implicit type 'float' for 'a' DIM a = 5 REM ' No warning here, because the compiler knows it is an integer (% suffix) DIM c% = 5REM 'a is not declared, but since you use PI, it must be float LET a = PIREM 'a here is taken as a FIXED not FLOAT. Beware with precision loss! LET a = 3.5
For any positive integer, unsigned types will be used.
REM ' a will be declared implicitly as BYTE FOR a = -1 TO 10 ... NEXT REM ' b will be declared as UByte FOR b = 1 TO 10 ... NEXT REM ' Warning, truncation! LET a = -3.5 REM ' Warning, sign overflow! LET b = -1
As you might see, using undeclared variables might lead to errors (truncation, overflow). The compiler will try to warning about these whenever it can, but sometimes this will be not possible, and errors might pass silently (you might experience strange behaviors in your program).
It might even be difficult for you to guess which type will be implicitly used for an undeclared variable. The safest choice is to always declare them.
You can declare a variable at a fixed memory address. This is called variable mapping.
E.g. address 23675 contains a system variable which points to UDG address. You could traditionally read this content by doing:
PRINT "UDG memory address is "; PEEK 23675 + 256 * PEEK 23676
It is a 16 bit unsigned integer value (UINTEGER). We can map a variable on that address:
DIM UDGaddr AS UINTEGER AT 23675 PRINT "UDG memory address is "; UDGaddr
This is more readable. Also, setting a value to this variable changes UDG address.
A variable is just a memory position containing data. In same cases you might find useful a variable having more than one name, for the sake of code readability:
DIM a AS FLOAT = PI REM 'Now let's declare an alias of 'a', called 'radians' DIM radians AS FLOAT AT @a PRINT "radians = "; radians LET radians = 1.5 PRINT "a = "; a
As you can see, both radians and a can be used interchangeably.
Notes:
See also:
Supported Plattforms:
ZX Spectrum